package com.puny.utils
{
	import com.puny.utils.ExpressionParser;
	
	import flexunit.framework.TestCase;

	public class ExpressionParserTest extends TestCase
	{
		public function ExpressionParserTest(methodName:String=null)
		{
			super(methodName);
		}
		
		public function test0():void {
			var parser:ExpressionParser = new ExpressionParser();
			assertNotNull(parser);
		}
		
		public function test1():void {
			var num:Number = ExpressionParser.calculate("1+4");
			assertEquals(5,num);
		}
		public function test2():void {
			var num:Number = ExpressionParser.calculate("1-4");
			assertEquals(-3,num);
		}
		public function test3():void {
			var num:Number = ExpressionParser.calculate("1/4");
			assertEquals(.25,num);
		}
		public function test4():void {
			var num:Number = ExpressionParser.calculate("1*4");
			assertEquals(4,num);
		}
		public function test5():void {
			var num:Number = ExpressionParser.calculate("-1*4");
			assertEquals(-4,num);
		}
		public function test6():void {
			var num:Number = ExpressionParser.calculate("1*-4");
			assertEquals(-4,num);
		}
		public function test7():void {
			var num:Number = ExpressionParser.calculate("2*(5+3)");
			assertEquals(16,num);
		}
		public function test8():void {
			var num:Number = ExpressionParser.calculate("2.3+1.1");
			assertEquals(3.4,num);
		}
		public function test9():void {
			var num:Number = ExpressionParser.calculate("400+200-500");
			assertEquals(100,num);
		}
		public function test10():void {
			var num:Number = ExpressionParser.calculate("400+200-500*2");
			assertEquals(-400,num);
		}
		
		public function test11():void {
			var num:Number =ExpressionParser.calculate("222");
			assertEquals(222,num);
		}
		
		/**
		 * Error cases
		 */
		public function testError1():void {
			var num:Number =ExpressionParser.calculate("2.2.2");
			assertTrue(isNaN(num));
		}
		public function testError2():void {
			var num:Number =ExpressionParser.calculate("happy");
			assertTrue(isNaN(num));
		}
		public function testError3():void {
			var num:Number =ExpressionParser.calculate("happy+3-4");
			assertTrue(isNaN(num));
		}
		public function testError4():void {
			var num:Number =ExpressionParser.calculate("(1-3*happy)");
			assertTrue(isNaN(num));
		}
		public function testError5():void {
			try {
			var num:Number =ExpressionParser.calculate("2+*1");
			} catch(e:Error) {
				assertTrue(e is SyntaxError);		
				assertEquals("Syntax Error around *", e.message);		
			}
			assertTrue(isNaN(num));
		}
		
		public function testError5_1():void {
			try {
			var num:Number =ExpressionParser.calculate("32-/1");
			} catch(e:Error) {
				assertTrue(e is SyntaxError);		
				assertEquals("Syntax Error around /", e.message);		
			}
			assertTrue(isNaN(num));
		}

		public function testError6():void {
			try {
				var num:Number =ExpressionParser.calculate("(2+1");
			} catch(e:Error) {
				assertTrue(e is SyntaxError);		
				assertEquals(") is missing.", e.message);		
			}
			assertTrue(isNaN(num));
		}
		
		public function testError7():void {
			try {
				var num:Number =ExpressionParser.calculate("1-3*60-");
			} catch(e:Error) {
				assertTrue(e is SyntaxError);		
				assertEquals("Syntax Error at the end.", e.message);		
			}
			assertTrue(isNaN(num));
		}

		public function testError8():void {
			try {
				var num:Number =ExpressionParser.calculate(")+2*5");
			} catch(e:Error) {
				assertTrue(e is SyntaxError);		
				assertEquals("Syntax Error around )", e.message);		
			}
			assertTrue(isNaN(num));
		}
		
		public function testError9():void {
			try {
				var num:Number =ExpressionParser.calculate("++2-+5");
			} catch(e:Error) {
				assertTrue(e is SyntaxError);		
				assertEquals("Syntax Error around +", e.message);		
			}
			assertTrue(isNaN(num));
		}
		
		public function testError10():void {
			try {
				var num:Number =ExpressionParser.calculate("3(4+2)");
			} catch(e:Error) {
				assertTrue(e is SyntaxError);		
				assertEquals("Syntax Error around (", e.message);		
			}
			assertTrue(isNaN(num));
		}
	}
}